﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.Geography
{
    /// <summary>
    /// TrustDistrictDetails
    /// </summary>
    public partial class TrustDistrictDetails : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitSaveMode();
            }
        }

        /// <summary>
        /// Inits the save mode.
        /// </summary>
        private void InitSaveMode()
        {
            int trustDistrictId;
            int trustRegionId;

            if (int.TryParse(Request.Params["trustRegionId"], out trustRegionId))
            {
                TrustRegion trustRegion = TrustRegionsBO.GetTrustRegionById(trustRegionId);
                if (trustRegion != null)
                {
                    ButtonSave.Enabled = true;
                    ButtonSave.CommandName = "new";

                    TextBoxTrustRegionName.Text = trustRegion.TrustRegionName;
                    HiddenFieldTrustRegionId.Value = trustRegionId.ToString(CultureInfo.CurrentCulture);
                }
            }

            if (int.TryParse(Request.Params["id"], out trustDistrictId))
            {
                TrustDistrict trustDistrict = TrustDistrictsBO.GetTrustDistrictsId(trustDistrictId);
                if (trustDistrict != null)
                {
                    ButtonSave.Enabled = true;
                    ButtonSave.CommandName = "edit";
                    ButtonInactive.Visible = trustDistrict.IsActive;

                    HiddenFieldTrustDistrictId.Value = trustDistrictId.ToString(CultureInfo.CurrentCulture);
                    HiddenFieldTrustDistrictName.Value = trustDistrict.TrustDistrictName;
                    HiddenFieldTrustRegionId.Value = trustDistrict.TrustRegionId.ToString(CultureInfo.CurrentCulture);

                    LoadTrustDistrict(trustDistrict);
                }
            }
        }

        /// <summary>
        /// Loads the trust district.
        /// </summary>
        /// <param name="trustDistrict">The trust district.</param>
        private void LoadTrustDistrict(TrustDistrict trustDistrict)
        {
            if (trustDistrict == null)
            {
                return;
            }

            HiddenFieldTrustDistrictId.Value = trustDistrict.TrustDistrictId.ToString(CultureInfo.CurrentCulture);
            HiddenFieldTrustDistrictName.Value = trustDistrict.TrustDistrictName;
            TextBoxTrustDistrictName.Text = trustDistrict.TrustDistrictName;
            TextBoxDescription.Text = trustDistrict.Description;

            if (trustDistrict.TrustRegion != null)
            {
                TrustRegion trustRegion = trustDistrict.TrustRegion;
                HiddenFieldTrustRegionId.Value = trustRegion.TrustRegionId.ToString(CultureInfo.CurrentCulture);
                TextBoxTrustRegionName.Text = trustRegion.TrustRegionName;
            }
        }

        /// <summary>
        /// Handles the Command event of the ButtonInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            int trustRegionId;
            if (int.TryParse(HiddenFieldTrustDistrictId.Value, out trustRegionId))
            {
                TrustDistrictsBO.SetTrustDistrictStatus(trustRegionId, false);
                ButtonInactive.Visible = false;
            }
        }

        /// <summary>
        /// Handles the Command event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Command(object sender, CommandEventArgs e)
        {
            string mode = e.CommandName;

            var trustDistrict = new TrustDistrict();
            int trustRegionId;
            trustDistrict.TrustDistrictName = TextBoxTrustDistrictName.Text;
            trustDistrict.TrustRegionId = int.TryParse(HiddenFieldTrustRegionId.Value, out trustRegionId)
                                              ? trustRegionId
                                              : 0;
            trustDistrict.Description = TextBoxDescription.Text;
            trustDistrict.TrustDistrictId = mode.Equals("edit")
                                                ? int.Parse(HiddenFieldTrustDistrictId.Value, CultureInfo.CurrentCulture)
                                                : 0;
            trustDistrict.IsActive = true;

            if (!HiddenFieldTrustDistrictName.Value.Equals(trustDistrict.TrustDistrictName) &&
                TrustDistrictsBO.CheckTrustDistrictName(trustDistrict.TrustDistrictName))
            {
                LabelCheckName.Text = "Trust District name must be unique";
                return;
            }

            bool result = e.CommandName.Equals("new")
                              ? TrustDistrictsBO.InsertTrustDistrict(trustDistrict)
                              : TrustDistrictsBO.EditTrustDistrict(trustDistrict);

            if (result)
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Geography/TrustRegionDetails.aspx?id={0}#DetailsPanel02",
                                                trustDistrict.TrustRegionId));
            }
            else
            {
                DialogBox.Error("Save TrustDistrict Successfully .");
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            int trustRegionId;
            if (int.TryParse(HiddenFieldTrustRegionId.Value, out trustRegionId))
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Geography/TrustRegionDetails.aspx?id={0}#DetailsPanel02",
                                                trustRegionId));
            }
        }
    }
}